package com.juntuo.platformUtils;


import com.juntuo.entity.Device;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DeviceMap2DeviceUtil {
    public static Device toDeviceData(Map<String,Object> deviceMap){
        Device device = new Device();
        device.setDeviceId((String) deviceMap.get("deviceId"));
        device.setGatewayId((String) deviceMap.get("gatewayId"));
        Map<String,Object> deviceInfo= (Map<String, Object>)deviceMap.get("deviceInfo");
        device.setOnline("ONLINE".equalsIgnoreCase((String)deviceInfo.get("status"))?1:0);
        device.setName((String)deviceInfo.get("name"));
        device.setModel((String)deviceInfo.get("model"));
        List<Map<String,Object>> services =(List<Map<String,Object>>) deviceMap.get("services");
        if(services!=null && services.size()>0){
            for(Map<String,Object> service:services){
                Map<String,Object> data = (Map<String,Object>)service.get("data");
                if("VehicleDetectorBasic".equals((String)service.get("serviceId"))){
                    device.setSequence((Integer) data.get("sequence"));
                    device.setStatus((String)data.get("status"));
                    device.setParkNumber((String)data.get("NO"));
                    device.setTemperature((Integer)data.get("temperature"));
                    device.setSignalStrength((Integer)data.get("signalStrength"));
                    if(data.get("latitude")!=null){
                        device.setLatitude(Double.parseDouble(data.get("latitude").toString()));
                    }
                    if(data.get("longitude")!=null){
                        device.setLongitude(Double.parseDouble(data.get("longitude").toString()));
                    }
                }else if("Battery".equals((String)service.get("serviceId"))){
                    device.setBatteryLevel((Integer)data.get("batteryLevel"));
                }
//                else if("Temperature".equals((String)service.get("serviceId"))){
//                    device.setTemperature((Integer)data.get("temperature"));
//                }else if("SignalStrength".equals((String)service.get("serviceId"))){
//                    device.setSignalStrength((Integer)data.get("signalStrength"));
//                }else if("Location".equals((String)service.get("serviceId"))){
//                    device.setLatitude(Double.parseDouble(data.get("latitude").toString()));
//                    device.setLongitude(Double.parseDouble(data.get("longitude").toString()));
//                }
            }
        }
        return device;
    }

    public static List<Device> toDeviceDatas(List<Map<String,Object>> deviceMaps){
        List<Device> devices = new ArrayList<>(deviceMaps.size());
        for(Map<String,Object> deviceMap:deviceMaps){
            Device device= DeviceMap2DeviceUtil.toDeviceData(deviceMap);
            devices.add(device);
        }
        return devices;
    }

}
